package com.dlwx.firefighting.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.bean.SignBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DateUtil;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnMonthChangedListener;
import com.prolificinteractive.materialcalendarview.spans.DotSpan;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SignInActivity extends BaseActivity {

    @BindView(R.id.calendarView)
    MaterialCalendarView calendarView;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    private String TAG = "CCC";
    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    private Calendar calendar;
    private String userId;
    private int requestNum = 31;
    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        initView();
        getDatas(1);
    }



    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        titleRightOneTv.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("每日签到");
        titleRightOneTv.setText("签到");
        calendar = Calendar.getInstance();
        calendarView.state().edit()
                        .setFirstDayOfWeek(Calendar.SUNDAY)
                        .setCalendarDisplayMode(CalendarMode.MONTHS)
                        .commit();
        calendarView.setSelectedDate(calendar.getTime());
        calendarView.setSelectionMode(MaterialCalendarView.SELECTION_MODE_NONE);

        String time= DateUtil.getCurrentDat1();
        String[] times=time.split(",");
        int year=Integer.valueOf(times[0]);
        final int month=Integer.valueOf(times[1]);
        int day=Integer.valueOf(times[2]);
        HashSet<CalendarDay> dates = new HashSet<>();
        dates.add(CalendarDay.from(year, month-1, day));
        EventDecorator1 ev = new EventDecorator1(R.color.fuzhu_green, dates);
        calendarView.addDecorators(ev);
        calendarView.setOnMonthChangedListener(new OnMonthChangedListener() {
            @Override
            public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
                int changeMonth=date.getMonth();
                if((changeMonth+1)<month){
                   int i= month-(changeMonth+1);
                    getDatas(i+1);
                }
            }
        });

    }

    @OnClick({R.id.title_left_one_btn, R.id.tv_middle_title,R.id.title_right_one_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_tv:
                signIn();
                break;
        }
    }


    private void getDatas(int page) {
        OkGo.<String>post(Constant.SIGN_LOG)
                .params("user_id", userId)
                .params("page", page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<SignBean> beans = dealResult.dealSign(SignInActivity.this, response, new TypeToken<BaseBean<SignBean>>() {
//                        }.getType());
//                        setDatas(beans);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(SignInActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void signIn() {
        OkGo.<String>post(Constant.SIGN_IN)
                .params("user_id", userId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        StatusBean bean= dealResult.dealOne(SignInActivity.this,response, new TypeToken<BaseOneBean<StatusBean>>(){}.getType());
//                        if(bean==null){
//                            return;
//                        }
//                        ToastUtils.toastForLong(SignInActivity.this,"恭喜您！签到成功,获得"+bean.getSign_money()+"余额");
//                        signToday();
                    }
                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(SignInActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
    private void signToday( ){
        String time= DateUtil.getCurrentDat1();
        String[] times=time.split(",");
        int year=Integer.valueOf(times[0]);
        int month=Integer.valueOf(times[1]);
        int day=Integer.valueOf(times[2]);
        HashSet<CalendarDay> dates = new HashSet<>();
        dates.add(CalendarDay.from(year, month-1, day));
        EventDecorator ev = new EventDecorator(R.color.fuzhu_red, dates);
        calendarView.addDecorators(ev);
    }
    List<SignBean> signBeens=new ArrayList<>();
    private void setDatas(List<SignBean> beans) {
        HashSet<CalendarDay> dates = new HashSet<>();
        if(beans.isEmpty()){
            return;
        }
        if(signBeens.containsAll(beans)){
            return;
        }
        signBeens.addAll(beans);
        for(SignBean bean:signBeens){
            dates.add(CalendarDay.from(bean.getYear(), bean.getMoon()-1,bean.getDay()));
//            LogUtils.d(bean.getYear()+bean.getMoon()+bean.getDay()+"");
        }
//        dates.add(CalendarDay.from(2017,10,14));
        EventDecorator ev = new EventDecorator(R.color.fuzhu_red, dates);
        calendarView.addDecorators(ev);
    }

    //定义选择颜色
    public class EventDecorator implements DayViewDecorator {
        private final int color;
        private final HashSet<CalendarDay> dates;

        public EventDecorator(int color, Collection<CalendarDay> dates) {
            this.color = color;
            this.dates = new HashSet<>(dates);
        }

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return dates.contains(day);
        }

        @Override
        public void decorate(DayViewFacade view) {
            view.addSpan(new DotSpan(10,Color.parseColor("#ff4323")));
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                view.setSelectionDrawable(getDrawable(R.drawable.bg_green_selector));
//            }
        }
    }
    //定义选择颜色
    public class EventDecorator1 implements DayViewDecorator {
        private final int color;
        private final HashSet<CalendarDay> dates;

        public EventDecorator1(int color, Collection<CalendarDay> dates) {
            this.color = color;
            this.dates = new HashSet<>(dates);
        }

        @Override
        public boolean shouldDecorate(CalendarDay day) {
            return dates.contains(day);
        }

        @Override
        public void decorate(DayViewFacade view) {
//            view.addSpan(new DotSpan(5, color));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                view.setSelectionDrawable(getDrawable(R.drawable.bg_green_border));
            }
        }
    }
}
